﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;

namespace Avatar.Person
{
    public static class JobApplicationRecordManager
    {
        /// <summary>
        /// 申请职位
        /// </summary>
        /// <param name="_applicant"></param>
        /// <param name="_jobID"></param>
        public static void apply(string _applicant, Guid _jobID)
        {
            #region 入口检测

            if (string.IsNullOrEmpty(_applicant)) throw new Exception("申请人不能为空");
            if (_jobID == Guid.Empty) throw new Exception("职位ID不能为空");
            
            #endregion

            var  _jar = GetRecord(_applicant,_jobID) ;
            if (_jar != null) throw new Exception("已申请过此职位");

            _jar = new JobApplicationRecord();
            _jar.Applicant = _applicant;
            _jar.JobID = _jobID;
            _jar.Date = DateTime.Now;

            DBHelper.Insert<JobApplicationRecord>(_jar, j => j.Applicant == _applicant && j.JobID == _jobID, "申请职位出错:");
        }

        /// <summary>
        /// 获取职位申请记录
        /// </summary>
        /// <param name="_applicant"></param>
        /// <param name="_jobID"></param>
        /// <returns></returns>
        public static JobApplicationRecord GetRecord(string _applicant, Guid _jobID)
        {
            return DBHelper.Get<JobApplicationRecord>(j=>j.Applicant==_applicant&&j.JobID==_jobID,"获取职位申请记录出错:");
        }

        /// <summary>
        /// 获取指定职位的申请记录
        /// </summary>
        /// <param name="_jobID"></param>
        /// <returns></returns>
        public static List<JobApplicationRecord> GetRecordList(Guid _jobID)
        {
            return DBHelper.GetAll<JobApplicationRecord>(j=>j.JobID==_jobID,"获取职位申请记录列表出错:");
        }
        /// <summary>
        /// 删除系统内三个月以前的所有用户职位申请记录
        /// </summary>
        /// <returns></returns>
        public static int ClearTheRecordThreeMonthsAgo()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 删除指定用户三个月前的所有职位申请记录
        /// </summary>
        /// <param name="_applicant"></param>
        /// <returns></returns>
        public static int ClearTheRecordThreeMonthsAgo(string _applicant)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取指定用户的职位申请记录
        /// </summary>
        /// <param name="_applicant"></param>
        /// <returns></returns>
        public static List<JobApplicationRecord> GetRecordList(string _applicant)
        {
            return DBHelper.GetAll<JobApplicationRecord>(r => r.Applicant == _applicant, "获取用户职位申请记录出错:").OrderByDescending(r => r.Date).ToList();
        }
        /// <summary>
        /// 获取指定职位申请人总数
        /// </summary>
        /// <param name="_jobID"></param>
        /// <returns></returns>
        public static int ApplicantNumber(Guid _jobID)
        {
            if (_jobID == Guid.Empty) throw new Exception("职位ID不能为空");

            return DBHelper.GetAll<JobApplicationRecord>(r=>r.JobID==_jobID,"获取指定职位申请人总数出错:").Count();
        }

        public static bool DeleteJobApplicationRecord(int _id)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                JobApplicationRecord _r = _adc.JobApplicationRecord.Where(r => r.ID == _id).FirstOrDefault();
                if (_r != null)
                {
                    _r.Shielding = true;

                    _adc.SubmitChanges();
                }
            }
            return true;
        }
    }
}
